<template>
	<view class="container">
		<view class="content">
			<!-- <radio-group name="radio-group" @change="null"> -->
				<view class="item" :class="index === current?'active':''" v-for="(item,index) in list"
					@click="change(item,index)">
					<view class="">
						<image :src="item.url" mode="widthFix" class="left-icon"></image>
					</view>
					<view class="name">{{item.title}}</view>
					<!-- <radio :value="item.status" :checked="index === current" /><text></text> -->
					<image :src="index === current?selImages[1]:selImages[0]" mode="widthFix" class="icon"></image>
				</view>


			<!-- </radio-group> -->
		</view>
		<view class="bottom">
			<view class="change">
				<button @click="showDialog()">确定</button>
			</view>



		</view>
	</view>
	<uni-popup ref="alertDialog" type="center" :mask-click="false" class="dialog">
		<view class="popup-content">

			<view class="title">
				温馨提示
			</view>
			<view class="content">
				修改为自助后，系统将不再自动结算，需要您在APP 中进行操作，请知悉!
				<view class="">
					修改后，系统将在<text class="blue">1小时内</text>完成更新阶段模式!
				</view>
			</view>
			<view class="buttonBox">
				<button @click="reset" class="button button-reset">取消</button>
				<button @click="confirm" class="button button-confirm">确定</button>
			</view>

		</view>

	</uni-popup>
	
	<uni-popup ref="smsDialog" type="center" :mask-click="false" class="smsDialog">
		<view class="sms-popup-content">
	<view class="">验证码</view>
			
			<view class="content">
				<view class="row ">
				
					<!-- <input v-model="phone" :placeholderStyle="placeholderStyle" placeholder="请输入手机号"
						class="value" />
					<view class="line"></view> -->
					
				</view>
				<view class="row border-none">
					<input v-model="params.smsCode" type="number" class="value" placeholder="请输入验证码" :maxlength="6" />
					<view @click="sendCode()" :disabled="isSend" size="mini"
						class="smscode">
						{{ buttonText }}
					</view>
					
				
				
				</view>
			</view>
			<view class="buttonBox">
				<button @click="closeSmsDialog()" class="button button-reset">取消</button>
				<button @click="goChange()" class="button button-confirm">确定</button>
			</view>
	
		</view>
	
	</uni-popup>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		withdrawSwitch,
		withdraw,
		withdrawSms,withdrawSwitchQuery
	} from "/src/api/mine.js"
	import {
		useStore
	} from "@/store/index";
	import {
		onLoad

	} from "@dcloudio/uni-app";
	import {
		showToast
	} from "@/utils/toast";
	/** store实例 */
	const store = useStore();
	const list = ref([{
		title: "自动结算",
		url: "/static/icon/settle_icon.png",
		status: '01'
	}, {
		title: "自助结算",
		url: "/static/icon/settle_2_icon.png",
		status: "00"
	}])
	const current = ref(0);
	const currentType = ref(null);
	const selImages=ref(["/static/icon/unsel_icon.png","/static/icon/sel_icon.png"])
	let params = ref({
		lsMerchantNo: store.currentMerchantInfomation.lsMerchantNo,
		status: "01",
		smsCode:null
	})
	let smsParams=ref({
		lsMerchantNo:store.currentMerchantInfomation.lsMerchantNo,
		status:"01"//00关 01开
	})
	let queryParams = ref({
		lsMerchantNo: store.currentMerchantInfomation.lsMerchantNo,
	
	})
	let phone=ref(null);
	let smsCode=ref(null);
	// 是否已发送验证码
		let isSend = ref(false);
		// 倒计时
		let seconds = ref(60);
		const buttonText=ref("发送验证码");
	function sendCode( ) {
		
		// if (!phone.value) {
		// 	showToast("请输入预留手机号");
		// 	return;
		// }
		
	
			withdrawSms(smsParams.value).then(() => {
				showToast(`发送验证码成功`, "success");
				isSend.value = true;
				buttonText.value = seconds.value + "s";
				let timer = setInterval(() => {
					if (seconds.value <= 0) {
						buttonText.value = "发送验证码";
						clearInterval(timer);
						timer = null;
						isSend.value = false;
						seconds.value = 60;
					} else {
						buttonText.value = seconds.value-- + "s";
					}
				}, 1000);
			}).catch((error) => {
				showToast(error.data.message);
			});
		}
	const alertDialog = ref();
	const smsDialog = ref();
	onLoad((val) => {
		console.log("store", JSON.stringify(store.currentMerchantInfomation.merchantNo))
		// if (store.currentMerchantInfomation) {
		// 	params.lsMerchantNo = store.currentMerchantInfomation.merchantNo
		// }
		queryWithdrawSwitch();
	
		
	});
function queryWithdrawSwitch(){
		withdrawSwitchQuery(queryParams.value).then((res) => {
				console.log("res-red", res)

	current.value=(res=="D0")?0:1;
	currentType.value=(res=="D0")?0:1;
			})
			.catch((err) => {

			});
	}
	function radioChange(e) {
		console.log("e=" + JSON.stringify(e))
		params.value.status = e.detail.value;
		smsParams.value.status = e.detail.value;
	}

	function change(item, index) {
		console.log("index="+index)
		if (current.value != index) {
			current.value = index;
			params.value.status = item.status;
			smsParams.value.status = item.status;
		}
	}

	

	function showDialog() {
		if(current.value==0){
			showSmsDialog();
		}else{
			alertDialog.value.open()
		}
		
	}
	function showSmsDialog() {
		smsDialog.value.open()
	}
	function goChange() {
if (!params.value.smsCode) {
			showToast("请输入验证码");
			return;
		}
		withdrawSwitch(params.value).then((res) => {
				console.log("res-red", res)
				if(params.status=="00"){
					uni.navigateTo({
								url: '/settle/withdraw/index'
							})
				}else{
					uni.navigateBack({
					  delta: 2
					  // 默认值是1，表示返回的页面层数
					});
				}

			})
			.catch((err) => {

			});
	}

	function reset() {
		alertDialog.value.close()
	}
	function closeSmsDialog() {
		smsDialog.value.close()
	}
	function confirm() {
		
		
		reset();
		if(currentType.value=current.value){
			if(currentType.value==1){
				uni.navigateTo({
					url: '/settle/withdraw/index'
				})
			}else{
				uni.navigateBack({
				  delta: 2
				  // 默认值是1，表示返回的页面层数
				});
			}
		}else{
			showSmsDialog();
		}
		
		
	}
</script>

<style lang="scss" scoped>
	@import "./index.scss";
</style>